import { getRoles } from '@/api/role'
import ro from 'element-ui/src/locale/lang/ro'
import { getGradesSys, getStages } from '@/api/basicmgt'
import { getTenants } from '@/api/tenant'
import { addStructure, getRoldsHXY, getStructure } from '@/api/hxy'
import { getTokenByName } from '@/utils/auth'
import store from './store'

// const VERSION ='v20210330_1'
// const VERSION ='v20210402_1'//学生/教师表增加了全部数据导出功能-导出当前页功能
const VERSION ='v20210408_1'//修复创建管理员时无法选择角色bug


//优化：导表接口报错500，展示报错信息给用户
let result_model = function(code, des) {
  this.code = code
  this.des = des
}

const PWS_STU_TEA ="Klmb321hbwx"

//统一返回代码--暂不启用20201120
const RESULT_CODE = {
  SUCCESS: new result_model(20000, '网络请求成功'),
  FAIL: new result_model(-20000, '网络请求失败'),
  ERROR: new result_model(-10000, '未知错误')
}

/**
 * 机顶盒上的业务使用的接口前缀IP
 * @type {string}
 */
const global = {
  VUE_APP_BASE_API_STB: process.env.VUE_APP_BASE_API
}

const defaultOrder = {
  id: '',
  order_type: '',  //套餐编码
  order_name: '',  //套餐名称
  price: '',
  auths: [],       //权限列表
  describe: ''     //描述
}

const COMBO_TYPE = [
  '月包-30天',
  '季包-90天',
  '年包-365天',
  '天次-7天'
]

var stage = []
/***
 * 学段
 * @type {function(): []}
 *
 * kcode编码规则：[STA][数字]
 * */
var STAGE = async function() {
  if (stage.length <= 0) {
    let res = await getStages()
    stage = res.data

  }
  return stage
}
// const STAGE = [
//   // {id:0,label:'小学',kcode:'STA001',desc:''},
//   // {id:1,label:'初中',kcode:'STA002',desc:''},
//   // {id:2,label:'高中',kcode:'STA003',desc:''},
// ]

var role = []

/**
 * 角色
 * @type {function(): []}
 */
var ROLE = async function() {
  if (role.length <= 0) {
    let res = await getRoles()
    role = res.data
    // console.log('ROLE==' + JSON.stringify(role))
  } else {
    // console.log("ROLE===="+JSON.stringify(role))
  }
  return role
}
/***
 * 组织机构列表 (租户列表)
 * @type {*[]}
 */

const CITY_LETTER = [
  { city: '石家庄', letter: 'A' },
  { city: '唐山市', letter: 'B' },
  { city: '秦皇岛市', letter: 'C' },
  { city: '邯郸市', letter: 'D' },
  { city: '邢台市', letter: 'E' },
  { city: '保定市', letter: 'F' },
  { city: '张家口市', letter: 'G' },
  { city: '承德市', letter: 'H' },
  { city: '沧州市', letter: 'J' },
  { city: '廊坊市', letter: 'R' },
  { city: '衡水市', letter: 'T' }
]

const STAGE_LETTER = [
  { stage: '幼儿园', letter: 'Y' },
  { stage: '小学', letter: 'X' },
  { stage: '初中', letter: 'C' },
  { stage: '高中', letter: 'G' }
]

// A代表：石家庄
// B代表：唐山市
// C代表：秦皇岛市bai
// D代表：邯郸市
// E代表：邢台市
// F代表：保定市
// G代表：张家口市
// H代表：承德市
// J代表：沧州市
// R代表：廊坊市
// T代表：衡水市

var organize = []

/***
 * 组织机构列表 (租户列表)
 * @type {{kcode: string, id: number, label: string}[]}
 *
 *
 * kcode 规则：[T:租户][X/C/G:小初高][主键]
 *
 * }
 */
var ORGANIZELIST = async function() {
  if (organize.length <= 0) {
    let res = await getTenants()
    console.log('organize=' + JSON.stringify(organize))

    organize = res.data
  } else {

  }
  return organize
}

const TEST_ORGANIZE = ORGANIZELIST[0]
/***
 * 组织机构的父级域
 *
 * @type {({id: number, label: string}|{id: number, label: string}|{id: number, label: string})[]}
 */
const DEPARTMENT = [
  { id: 0, label: '系统管理员' },
  { id: 1, label: '运营管理员' },
  { id: 2, label: '校级管理员' }
]

/***
 * 套餐
 * @type {*[]}
 */
const ORDER_TYPE = [
  {//套餐模型
    order_type: '',  //套餐编码
    order_name: '',  //套餐名称
    price: '',          //价格
    auths: [1, 2, 3, 4, 5, 6],       //权限列表
    describe: ''     //描述
  }
]

/**
 * 年级选项
 * @type {({id: string, label: string}|{id: string, label: string}|{id: string, label: string}|{id: string, label: string}|{id: string, label: string})[]}
 */
// const GRADE = [
//   { id: '0', label: '未选择' },
//   { id: '1', label: '一年级' },
//   { id: '2', label: '二年级' },
//   { id: '3', label: '三年级' },
//   { id: '4', label: '四年级' },
//   { id: '5', label: '五年级' },
//   { id: '6', label: '六年级' }
// ]

var gradeSys = []
const GRADE_SYS = async function() {
  if (gradeSys.length <= 0) {
    let res = await getGradesSys()
    gradeSys = res.data
  }

  return gradeSys
}

/***
 * 班级数据；具体班级显示方式由学校自拟
 * @type {{id: string, label: string}[]}
 */
const CLASS = [
  // { id: '0', label: '未选择' },
  // { id: '1', label: '1' },
  // { id: '2', label: '2' },
  // { id: '3', label: '3' },
  // { id: '4', label: '4' },
  // { id: '5', label: '5' },
  // { id: '6', label: '6' },
  // { id: '7', label: '7' },
  {
    id: '',
    kcode: 'TAX0001GL001C1',//班级编码（唯一）规则：组织架构code+年级编码+C+序号
    grade_code: 'GL001',//年级编码
    organize_code: 'TAX0001',//组织架构code
    room_code: '',//教室编码，对应摄像头
    description: ''
  }

]

const SEX = [
  { id: '0', label: '男' },
  { id: '1', label: '女' }
]

/**
 * 绑定选项
 * @type {({id: string, label: string, type: string}|{id: string, label: string, type: string}|{id: string, label: string, type: string})[]}
 */
const BIND_STATUS = [
  { id: '0', type: 'info', label: '未绑定' },
  { id: '1', type: 'success', label: '已绑定' }
  // { id: '2', type: 'danger', label: '未激活' }
]

/**
 * 支付选项
 * @type {({id: string, label: string, type: string}|{id: string, label: string, type: string}|{id: string, label: string, type: string})[]}
 */
const PAY_STATUS = [
  { id: '0', type: 'danger', label: '欠费' },
  { id: '1', type: 'success', label: '续存' },
  { id: '2', type: 'info', label: '未激活' }
]

/**
 * 家长选项名称与ID对应关系，固定值
 * @type {({id: number, label: string, type: string}|{id: number, label: string, type: string})[]}
 */
const ROLE_PARENTS_NAME = [
  { id: 0, type: 'danger', label: '未绑定' },
  { id: 1, type: 'info', label: '妈妈' },
  { id: 2, type: 'info', label: '爸爸' },
  { id: 3, type: 'info', label: '爷爷' },
  { id: 4, type: 'info', label: '奶奶' },
  { id: 5, type: 'info', label: '姥姥' },
  { id: 6, type: 'info', label: '姥爷' },
  { id: 7, type: 'info', label: '其他' }
]

const ACTIVE = [
  { id: 0, type: 'info', label: '禁用' },
  { id: 1, type: 'success', label: '启用' }
]

const TEMPLATE_EXCEL = [
  'https://iptv.zhixue100.com.cn/student_2007以上版本.xlsx',
  'https://iptv.zhixue100.com.cn/teacher_2007以上版本.xlsx'
]
const UPLOAD_EXCEL = [
  process.env['VUE_APP_BASE_API']+'/uploadStudent',
  process.env['VUE_APP_BASE_API']+'/uploadTeacher'
]

const rolesize={
  RA0003:[],
  RA0004:[],
  RA0005:[],
}


const USER = {}
//-------------------------------------华栖云----------------------------------------
/***
 * 根据租户获取默认组织结构（仅用于本系统管理员用户创建）
 * @returns {Promise<*>}
 * @constructor
 */
const GET_HXY_STRUCTURE = async function(tenandname) {
  let tenantid = store.getters.user_organize

  let { data } = await getStructure()
  // console.log("select==="+JSON.stringify(data).indexOf(tenandname))
  if (JSON.stringify(data).toString().indexOf(tenandname) < 0) {//无对应字符，则无此组织架构
    await addStructure({ display_name: tenandname }).then(res => {
      // console.log('创建组织架构：' + res.message + '===' + res.params.id)
      // return res.params.id
      return res.params.display_name
    })
  }

  let params = data.params
  // console.log('++++++=' + params.list)
  // console.log('++++++=' + store.getters.user_organize)

  if (params.list.length > 0) {
    for (let index in params.list) {
      console.log("display_name="+JSON.stringify(params.list[index].display_name))
      if (params.list[index].display_name == tenandname) {
        return params.list[index].display_name
      }
    }
  }

}

const CHANGE_TENANT = async function(tenantid) {

  console.log("租户登录："+getTokenByName('hxy_pwd'))

  let hxyUser = {
    account: store.getters.name,
    password: getTokenByName('hxy_pwd'),
    tenant_id: tenantid,
    // bindcode:store.getters.name
    bindcode: tenantid
  }
  return await store.dispatch('user/loginHXY', hxyUser)
}

/**
 * RA0001:角色编码
 * {}华栖云默认角色信息（他要是改了就fuck了）
 * @type {{RA0003: {name: string, kcode: string, id: string}, RA0002: {name: string, kcode: string, id: string}, RA0001: {name: string, kcode: string, id: string}, RA1001: {name: string, kcode: string, id: string}, RA1002: {name: string, kcode: string, id: string}}}
 */
let HXY_ROLE = {
  RA0001: { id: 12, name: '系统管理员', kcode: 'RA0001' },
  RA0002: { id: 13, name: '教务管理员', kcode: 'RA0002' },
  RA0003: { id: 13, name: '教务管理员', kcode: 'RA0002' },
  RA1001: { id: 14, name: '教师', kcode: 'RA1001' },
  RA1002: { id: 15, name: '学生', kcode: 'RA1002' }
}

let GRADE_LOCAL = {
  GL001: '一年级',
  GL002: '二年级',
  GL003: '三年级',
  GL004: '四年级',
  GL005: '五年级',
  GL006: '六年级',
  GL007: '七年级',
  GL008: '八年级',
  GL009: '九年级',
  GL010: '高一',
  GL011: '高二',
  GL012: '高三'
}


/**
 * 根据角色编码获取hxy角色id
 * @param name 角色编码
 * @returns {*}
 */
const getROLE_ID_HXY = async function(kcode) {

  let { data } = await getRoldsHXY()
  // console.log("getROLE_ID_HXY="+JSON.stringify(data))
  // console.log("getROLE_ID_HXY="+kcode)
  let params = data.params
  if (params.list.length > 0) {
    for (let index in params.list) {
      // console.log("display_name="+params.list[index].display_name+"=="+ HXY_ROLE[kcode].name)
      if (params.list[index].display_name == HXY_ROLE[kcode].name) {
        // console.log("display_name="+params.list[index].display_name+"=="+ params.list[index].id)
        return params.list[index].display_name
      }
    }
  }
  // return HXY_ROLE[kcode].id
}

export default {
  global,
  STAGE_LETTER,
  CITY_LETTER,
  defaultOrder,
  ORGANIZELIST,
  ORDER_TYPE,
  GRADE_SYS,
  BIND_STATUS,
  PAY_STATUS,
  ROLE_PARENTS_NAME,
  SEX,
  CLASS,
  ACTIVE,

  STAGE,
  DEPARTMENT,
  ROLE,
  TEMPLATE_EXCEL,
  UPLOAD_EXCEL,
  COMBO_TYPE,
  GET_HXY_STRUCTURE,
  CHANGE_TENANT,
  getROLE_ID_HXY,
GRADE_LOCAL,
  PWS_STU_TEA,
  VERSION

}
